package com.xnccc.fragment;

import com.owen.mapx.MapX;
import com.xnccc.audioset.MainActivity;
import com.xnccc.audioset.R;
import com.xnccc.view.VerticalSeekBar;
import com.xnccc.view.VerticalSeekBar.OnSeekBarChangeListener;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class NoiseFragment extends Fragment implements OnSeekBarChangeListener, OnClickListener {

	private boolean isOpen1 = false, isOpen2 = false, isOpen3 = false, isOpen4 = false, isOpen5 = false,
			isOpen6 = false;

	int show[] = { R.id.txtshow1, R.id.txtshow2, R.id.txtshow3, R.id.txtshow4, R.id.txtshow5, R.id.txtshow6 };
	int bars[] = { R.id.seekbar1, R.id.seekbar2, R.id.seekbar3, R.id.seekbar4, R.id.seekbar5, R.id.seekbar6 };
	TextView[] showTxts = new TextView[6];
	VerticalSeekBar seekBars[] = new VerticalSeekBar[6];
	private Button btn1, btn2, btn3, btn4, btn5, btn6;
	private MapX bird;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		bird = MainActivity.bird;
		// bird.setDPL2(false);
		// bird.setBassMng(false);
		View mView = inflater.inflate(R.layout.fragment_noise, container, false);
		initView(mView);
		return mView;
	}

	private void initView(View mView) {

		Log.e("第一根1", bird.getVol(MapX.CH_L) + "");
		for (int i = 0; i < bars.length; i++) {
			showTxts[i] = (TextView) mView.findViewById(show[i]);
			seekBars[i] = (VerticalSeekBar) mView.findViewById(bars[i]);
			seekBars[i].setOnSeekBarChangeListener(this);
		}

		seekBars[0].setProgress(30 - bird.getVol(MapX.CH_L));
		seekBars[1].setProgress(30 - bird.getVol(MapX.CH_C));
		seekBars[2].setProgress(30 - bird.getVol(MapX.CH_R));
		seekBars[3].setProgress(30 - bird.getVol(MapX.CH_SR));
		seekBars[4].setProgress(30 - bird.getVol(MapX.CH_SL));
		seekBars[5].setProgress(30 - bird.getVol(MapX.CH_LFE));

		showTxts[0].setText(bird.getVol(MapX.CH_L) + "");
		showTxts[1].setText(bird.getVol(MapX.CH_C) + "");
		showTxts[2].setText(bird.getVol(MapX.CH_R) + "");
		showTxts[3].setText(bird.getVol(MapX.CH_SR) + "");
		showTxts[4].setText(bird.getVol(MapX.CH_SL) + "");
		showTxts[5].setText(bird.getVol(MapX.CH_LFE) + "");

		Log.e("第一根2", showTxts[0].getText().toString());

		btn1 = (Button) mView.findViewById(R.id.noise1);
		btn2 = (Button) mView.findViewById(R.id.noise2);
		btn3 = (Button) mView.findViewById(R.id.noise3);
		btn4 = (Button) mView.findViewById(R.id.noise4);
		btn5 = (Button) mView.findViewById(R.id.noise5);
		btn6 = (Button) mView.findViewById(R.id.noise6);

		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);
		btn5.setOnClickListener(this);
		btn6.setOnClickListener(this);
	}

	private void chanage(int i, int dd, int vol) {
		showTxts[i].setText(vol + "");
		bird.setVol(dd, vol);
		Log.e("c123", "i:" + i + ",vol:" + vol);
	}

	@Override
	public void onProgressChanged(VerticalSeekBar VerticalBar, int progress, boolean fromUser) {
		if (fromUser) {
			switch (VerticalBar.getId()) {
			case R.id.seekbar1:
				chanage(0, MapX.CH_L, 30 - progress);
				break;
			case R.id.seekbar2:
				chanage(1, MapX.CH_C, 30 - progress);
				break;
			case R.id.seekbar3:
				chanage(2, MapX.CH_R, 30 - progress);
				break;
			case R.id.seekbar4:
				chanage(3, MapX.CH_SR, 30 - progress);
				break;
			case R.id.seekbar5:
				chanage(4, MapX.CH_SL, 30 - progress);
				break;
			case R.id.seekbar6:
				chanage(5, MapX.CH_LFE, 30 - progress);
				break;
			}
		}
	}

	@Override
	public void onStartTrackingTouch(VerticalSeekBar VerticalBar) {

	}

	@Override
	public void onStopTrackingTouch(VerticalSeekBar VerticalBar) {

	}

	private void changeBtn() {
		btn1.setText("Off");
		btn2.setText("Off");
		btn3.setText("Off");
		btn4.setText("Off");
		btn5.setText("Off");
		btn6.setText("Off");
	}

	@Override
	public void onClick(View v) {
		changeBtn();
		switch (v.getId()) {
		case R.id.noise1:
			isOpen1 = !isOpen1;
			if (isOpen1) {
				bird.switchSRC(MapX.SRC_NOISE);
				changeState(1);
				btn1.setText("On");
				bird.setNoiseChannel(MapX.CH_L);
			} else {
				bird.switchSRC(MapX.SRC_ARM);
				btn1.setText("Off");
				bird.setNoiseChannel(MapX.CH_NULL);
			}
			break;
		case R.id.noise2:
			isOpen2 = !isOpen2;
			if (isOpen2) {
				bird.switchSRC(MapX.SRC_NOISE);
				btn2.setText("On");
				changeState(2);
				bird.setNoiseChannel(MapX.CH_C);
			} else {
				bird.switchSRC(MapX.SRC_ARM);
				btn2.setText("Off");
				bird.setNoiseChannel(MapX.CH_NULL);
			}
			break;
		case R.id.noise3:
			isOpen3 = !isOpen3;
			if (isOpen3) {
				bird.switchSRC(MapX.SRC_NOISE);
				btn3.setText("On");
				changeState(3);
				bird.setNoiseChannel(MapX.CH_R);
			} else {
				bird.switchSRC(MapX.SRC_ARM);
				btn3.setText("Off");
				bird.setNoiseChannel(MapX.CH_NULL);
			}
			break;
		case R.id.noise4:
			isOpen4 = !isOpen4;
			if (isOpen4) {
				bird.switchSRC(MapX.SRC_NOISE);
				btn4.setText("On");
				changeState(4);
				bird.setNoiseChannel(MapX.CH_SR);
			} else {
				bird.switchSRC(MapX.SRC_ARM);
				btn4.setText("Off");
				bird.setNoiseChannel(MapX.CH_NULL);
			}
			break;
		case R.id.noise5:
			isOpen5 = !isOpen5;
			if (isOpen5) {
				bird.switchSRC(MapX.SRC_NOISE);
				btn5.setText("On");
				changeState(5);
				bird.setNoiseChannel(MapX.CH_SL);
			} else {
				bird.switchSRC(MapX.SRC_ARM);
				btn5.setText("Off");
				bird.setNoiseChannel(MapX.CH_NULL);
			}
			break;
		case R.id.noise6:
			isOpen6 = !isOpen6;
			if (isOpen6) {
				bird.switchSRC(MapX.SRC_NOISE);
				btn6.setText("On");
				changeState(6);
				bird.setNoiseChannel(MapX.CH_LFE);
			} else {
				bird.switchSRC(MapX.SRC_ARM);
				btn6.setText("Off");
				bird.setNoiseChannel(MapX.CH_NULL);
			}
			break;
		}
	}

	private void changeState(int which) {
		isOpen1 = isOpen2 = isOpen3 = isOpen4 = isOpen5 = isOpen6 = false;
		switch (which) {
		case 1:
			isOpen1 = true;
			break;
		case 2:
			isOpen2 = true;
			break;
		case 3:
			isOpen3 = true;
			break;
		case 4:
			isOpen4 = true;
			break;
		case 5:
			isOpen5 = true;
			break;
		case 6:
			isOpen6 = true;
			break;

		}
	}
}
